﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;
using System.Collections;


public partial class admin_newsletter_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {            
            int pageIndex = ConvertUtility.ToInt32(Request.QueryString["page"]);
            if (pageIndex > 0)
                bindUser(pageIndex);
            else
                bindUser(1);
            lblMessage.Text = "";            
        }
        catch (Exception ex) { }
    }
    protected void bindUser(int pageIndex)
    {
        try
        {
            string text = hidChar.Value;
            memberLet_Info[] lstUser = memberLet_Info.FindAll(Order.Desc("id"), Expression.Like("hoTen",text,MatchMode.Start));
            repUser.DataSource = lstUser.Skip((pageIndex - 1) * 10).Take(10);
            repUser.DataBind();
            int totalPage = (int)Math.Ceiling(ConvertUtility.ToDouble(lstUser.Length) / 10);
            pagerbot.Text = ConvertUtility.buidPaging3(totalPage, pageIndex, HttpContext.Current.Request.Url.LocalPath);
        }
        catch (Exception ex) { }
    }
    protected void repUser_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            int id = ConvertUtility.ToInt32(e.CommandArgument);
            memberLet_Info memInfo = memberLet_Info.Find(id);
            if (e.CommandName == "yes")
            {
                memInfo.isActive = true;
                memInfo.SaveAndFlush();

            }
            else if (e.CommandName == "no")
            {
                memInfo.isActive = false;
                memInfo.SaveAndFlush();
            }
            Response.Redirect("/admin/newsletter/default.aspx");
        }
        catch (Exception ex) { }
    }
    protected void repUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
        LinkButton lnkUnActive = (LinkButton)e.Item.FindControl("lnkUnActive");
        HiddenField hidIsActive = (HiddenField)e.Item.FindControl("hidIsActive");
        if (ConvertUtility.ToBoolean(hidIsActive.Value) == true)
        {
            lnkActive.Enabled = false;
            lnkUnActive.Enabled = true;
            lnkActive.CssClass = "disable";
            lnkUnActive.CssClass = "enable";
        }
        else
        {
            lnkActive.Enabled = true;
            lnkUnActive.Enabled = false;
            lnkActive.CssClass = "enable";
            lnkUnActive.CssClass = "disable";
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            string[] arrKeyDel = hidKeyDel.Value.Split('|');

            for (int i = 0; i < arrKeyDel.Length - 1; i++)
            {
                memberLet_Info contenInfo = memberLet_Info.Find(Convert.ToInt32(arrKeyDel[i].Trim()));
                contenInfo.DeleteAndFlush();
            }
            Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);

        }
        catch (Exception ex)
        {
            lblMessage.Text = "Có lỗi xảy ra. Hãy thử lại";
            fail.Visible = true;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bindUser(1);
    }
}